﻿using EnterpriseWebsiteEntity;
using EnterpriseWebsiteUtil.Helper;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;

namespace EnterpriseWebsiteBLL
{
    public class ArticleService
    {
        public readonly SqlSugarClient _db;

        public ArticleService(SqlSugarHelper sqlSugarHelper)
        {
            _db = sqlSugarHelper.sugarClient;
        }

        public async Task<List<ArticleEntity>> GetAllArticlesAsync(RefAsync<int> total, int index = 0,int pageSize = 20, int category = 0, string keyword = null, bool? display = null)
        {
            var queryable = _db.Queryable<ArticleEntity>();

            if (category != 0)
            {
                queryable = queryable.Where(a => a.Category == category);
            }

            if (!string.IsNullOrWhiteSpace(keyword))
            {
                queryable = queryable.Where(a => a.Title.Contains(keyword) || a.Content.Contains(keyword));
            }

            queryable.WhereIF(display != null, q => q.Display == display);
            queryable.OrderByDescending(q => q.CreateDate);

            return await queryable.ToPageListAsync(index, pageSize, total);
        }

        public async Task<ArticleEntity> GetArticleByIdAsync(int id)
        {
            return await _db.Queryable<ArticleEntity>().InSingleAsync(id);
        }

        public async Task AddArticleAsync(ArticleEntity article)
        {
            if (string.IsNullOrWhiteSpace(article.Title) || string.IsNullOrWhiteSpace(article.Content))
            {
                throw new Exception("标题和内容为必填字段。");
            }
            article.Author = "admin";
            article.CreateDate = DateTime.Now; // 设置创建时间
            article.ModifiedDate = DateTime.Now; // 设置更新时间
            await _db.Insertable(article).ExecuteCommandAsync();
        }

        public async Task UpdateArticleAsync(ArticleEntity article)
        {
            if (string.IsNullOrWhiteSpace(article.Title) || string.IsNullOrWhiteSpace(article.Content))
            {
                throw new Exception("标题和内容为必填字段。");
            }
            article.ModifiedDate = DateTime.Now; // 设置更新时间
            await _db.Updateable(article).ExecuteCommandAsync();
        }

        public async Task DeleteArticleAsync(int id)
        {
            await _db.Deleteable<ArticleEntity>().In(id).ExecuteCommandAsync();
        }

        public async Task DeleteArticleBatchAsync(IEnumerable<int> idArray)
        {
            await _db.BeginTranAsync();
            foreach (var id in idArray)
            {
                var entity = await GetArticleByIdAsync(id);
                if (entity == null)
                {
                    await _db.RollbackTranAsync();
                    throw new Exception($"Id {id}，文章不存在");
                }
                await DeleteArticleAsync(id);
            }
            await _db.CommitTranAsync();
        }

        public async Task ChangeDisplay(int id, bool display)
        {
            var entity = await GetArticleByIdAsync(id);
            if (entity == null)
            {
                throw new Exception($"Id {id}，文章不存在");
            }
            entity.Display = display;
            _db.Updateable(entity).ExecuteCommand();
        }
    }
}
